<template>
  <div class="index">
    <!--head-->
    <div class="role-head">
      <p class="role-head-title"><i class="icon iconfont icon-zidian"></i>数据字典</p>
      <div class="role-head-operation">
        <span class="role-head-operation-items" v-if="dictionariesRead" @click="selectShow"><i class="icon iconfont icon-chaxun"></i><span style="display:block;float:left">查询</span></span>
        <span class="role-head-operation-items" v-if="dictionariesAdd" @click="toAdd"><i class="icon iconfont icon-xinzeng"></i><span style="display:block;float:left">新增</span></span>
        <span class="role-head-operation-items"><a style="color:#fff;" href="javascript:history.go(-1)"><span style="display:block;float:left;font-size:16px;">返回</span><i class="icon iconfont icon-jinru"></i></a></span>
      </div>
    </div>
    <!--head end-->
    <!--select-->
    <div class="role-select on">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form label-position="left" label-width="80px" class="aaa">
            <el-form-item label="分类名称" class="rolelabel">
              <el-input class="role-select-ipt" v-model="select.className"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-form label-position="left" label-width="80px">
            <el-form-item label="实例名称" class="rolelabel">
              <el-input class="role-select-ipt" v-model="select.instanceName"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="12">
          <el-form label-position="left" label-width="80px">
            <el-form-item label="字典标识" class="rolelabel">
              <el-input class="role-select-ipt" v-model="select.dicMark"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" :offset="16">
            <el-button @click="selectHide" class="colse">关    闭</el-button>
            <el-button class="select-submit" @click="getDictionary">查    询</el-button>
        </el-col>
      </el-row>      
    </div>
    <!--select end-->
    <!--table-->
    <div class="role-table">
      <table id="role-table">
          <tbody class="role-table-title">
            <td>类名称</td>
            <td>实例名称</td>
            <td>字典标识</td>
            <td>字典名称</td>
            <td style="text-indent:18px;">操作</td>
          </tbody>
          <tr v-if="empty">
            <td class="empty" colspan="5">暂无数据</td>
          </tr>
          <tr v-else v-for="items in dictionaryList">
            <td>{{items.className}}</td>
            <td>{{items.instanceName}}</td>
            <td>{{items.dicMark}}</td>
            <td>{{items.dicName}}</td>
            <td>
              <div class="role-table-operation">
                <i class="icon iconfont icon-bianji" v-if="dictionariesEdit" @click="toEdit(items.dicBh)" title="编辑"></i>
                <i class="icon iconfont icon-shanchu" v-if="dictionariesDelete" @click="toDelete(items.dicBh, $el)" title="删除"></i>
              </div>
            </td>
          </tr>
      </table>
    </div>
    <!--table end-->
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="13"
      layout="prev, pager, next, jumper"
      :total="parseInt(total)"><!--因为这里:total期望的值是Number类型，但是vue返回的是String类型，所以要parseInt()转换类型-->
    </el-pagination>
    <!--分页 end-->
    <!--Add-->
    <roleAdd ref="Add" v-if="Addshow"></roleAdd>
    <!--rAdd end-->
    <!--Add-->
    <dictionaryEdit ref="Edit" v-if="Editshow" :message="personalDec"></dictionaryEdit>
    <!--rAdd end-->
  </div>
</template>

<script>
import roleAdd from "./../components/dictionaryAdd";
import dictionaryEdit from "./../components/dictionaryEdit";
import axios from "axios";
export default {
  name: "dictionary",
  components: {
    roleAdd,
    dictionaryEdit
  },
  data() {
    return {
      currentPage: 1,
      total: "",
      Addshow: false,
      Editshow: false,
      empty: false,
      dictionariesDelete: "",
      dictionariesEdit: "",
      dictionariesAdd: "",
      dictionariesRead: "",
      select: {
        className: "", //分类名称
        instanceName: "", //实例名称
        dicMark: "", //字典标识
        currentPage: "", //当前页默认1
        pageSize: 13, //每页行数默认1
        sortby: "", //排序列,数据列的某一列
        order: "asc" //排序方式,正序：asc，倒序：desc
      },
      dictionaryList: [],
      personalDec: {} //存放单个字典信息
    };
  },
  mounted: function() {
    this.$nextTick(() => {
      //防止在登陆页没有登陆时候直接手动改变url访问
      this.initPage();
      //调用全局方法 设置index高度
      this.indexHeight();
      //表格高度
      $(".role-table").height($(window).height() - 330 + "px");
      //
      this.getDictionary();
      //调用全局方法根据权限值显示对应信息
      this.grantType();
    });
  },
  methods: {
    handleCurrentChange(val) {
      this.currentPage = val;
      axios
        .get(dictionariesURL, {
          headers: { Authorization: appDB.get("userInfo").token },
          params: {
            currentPage:this.currentPage,
            pageSize: 13
          }
        })
        .then(response => {
          let d = response.data;
          if (d.resultCode == 0) {
            if (d.record.rows.length > 0) {
              this.dictionaryList = d.record.rows;
            } else {
              this.empty = true;
            }
          } else {
            layer.msg(d.msg);
          }
        })
        .catch(error => {
          layer.msg("网络错误");
        });
    },
    //查条件hide
    selectShow() {
      $(".role-select").removeClass("on");
    },
    //查询条件hide
    selectHide() {
      $(".role-select").addClass("on");
      this.select.className = "";
      this.select.instanceName = "";
      this.select.dicMark = "";
      this.getDictionary();
    },
    //新增
    toAdd() {
      this.Addshow = true;
      this.$nextTick(() => {
        let that = this;
        layer.open({
          type: 1,
          shadeClose: true,
          title: "新增字典",
          skin: "layui-layer-rim", //加上边框
          area: ["600px", "500px"], //宽高
          content: $(this.$refs.Add.$el),
          end: function() {
            that.Addshow = false;
          }
        });
      });
    },
    //编辑
    toEdit(dicBh) {
      this.Editshow = true;
      this.grtPerDEC(dicBh);
      this.$nextTick(() => {
        let that = this;
        layer.open({
          type: 1,
          shadeClose: true,
          title: "编辑字典",
          skin: "layui-layer-rim", //加上边框
          area: ["600px", "500px"], //宽高
          content: $(this.$refs.Edit.$el),
          end: function() {
            that.Editshow = false;
          }
        });
      });
    },
    //delete
    toDelete(dicBh, el) {
      axios
        .delete(dictionariesURL + "/" + dicBh, {
          headers: { Authorization: appDB.get("userInfo").token }
        })
        .then(res => {
          let d = res.data;
          if (d.resultCode == 0) {
            this.getDictionary(); //重新渲染列表
          } else {
            layer.msg(d.msg);
          }
        });
    },
    //获取字典列表
    getDictionary() {
      axios
        .get(dictionariesURL, {
          headers: { Authorization: appDB.get("userInfo").token },
          params: this.select
        })
        .then(response => {
          let d = response.data;
          if (d.resultCode == 0) {
            this.total = d.record.totalPage;            
            if (d.record.rows.length > 0) {
              this.dictionaryList = d.record.rows;
            } else {
              this.empty = true;
            }
          } else {
            layer.msg(d.msg);
          }
        })
        .catch(error => {
          layer.msg("网络错误");
        });
    },
    //获取单个字典信息
    grtPerDEC(dicBh) {
      axios
        .get(dictionariesURL + "/" + dicBh, {
          headers: { Authorization: appDB.get("userInfo").token }
        })
        .then(res => {
          let d = res.data;
          if (d.resultCode == 0) {
            this.personalDec = d.record;
          } else {
            layer.msg(d.msg);
          }
        })
        .catch(error => {
          layer.msg("网络错误");
        });
    }
  }
};
</script>

<style scoped>
@import "../assets/css/elementUI.css";
.el-pagination {
  margin-top: 15px;
}
.role-head {
  width: 100%;
  float: left;
  color: #fff;
  margin: 70px 0 20px 0;
}
.role-head-title {
  float: left;
  font-size: 16px;
}
.icon-zidian {
  font-size: 20px;
  margin-right: 8px;
}
.role-head-operation {
  /* width: 258px; */
  float: right;
}
.role-head-operation-items {
  height: 18px;
  line-height: 18px;
  padding: 0 20px;
  display: inline-block;
  cursor: pointer;
  border-right: 1px solid #fff;
  float: left;
}
.role-head-operation-items:last-child {
  float: left;
  border-right: 0;
  font-size: 16px;
  padding-right: 0;
}
.icon-chaxun {
  display: inline-block;
  margin-right: 4px;
  float: left;
}
.icon-xinzeng {
  display: inline-block;
  margin-right: 4px;
  font-size: 18px;
  float: left;
}
.icon-jinru {
  color: #35c126;
  font-size: 26px;
  display: inline-block;
}
.role-table {
  width: 100%;
  float: left;
  background: #fff;
  overflow-y: auto;
}
#role-table {
  width: 100%;
  border: 0;
  background: #fff;
  float: left;
}
#role-table tr td {
  padding: 12px 15px;
  border: 1px solid #cbcbcb;
}
#role-table tr:hover {
  background: #f0f0f0;
}
.role-table-title {
  width: 100%;
  background: #0a456f;
  color: #fff;
}
.role-table-title td {
  padding: 12px 15px;
  border-right: 1px solid #012842;
  border-bottom: 1px solid #cbcbcb;
}
.role-table-operation {
  display: flex;
  justify-content: space-around;
}
.role-table-operation i {
  color: #5594d1;
  font-size: 18px;
  cursor: pointer;
}
.role-select {
  width: 100%;
  height: 206px;
  padding: 20px 4%;
  overflow: hidden;
  float: left;
  background: #14466a;
  margin-bottom: 20px;
  transition: all ease-in-out 0.5s;
}
.role-select.on {
  width: 100%;
  height: 0;
  padding: 0 4%;
  background: #14466a;
  margin-bottom: 0;
}
.colse {
  border-radius: 30px;
  padding: 8px 40px;
  background: none;
  border: 1px solid #367fb6;
  color: #fff;
}
.select-submit {
  border-radius: 30px;
  padding: 8px 40px;
  background: #39b423;
  border: 1px solid #39b423;
  color: #fff;
}
</style>
